﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace HtmlMultiFileEditor.Model
{
    public class FileHandler
    {

        #region "Singleton"

        private static FileHandler fileHandler = new FileHandler();

        private FileHandler() { }

        public static FileHandler Instance
        {
            get { return fileHandler; }
        }

        #endregion

        #region "Private"

        private string filePath = String.Empty;
        private string[] fileList;
        private string[] fileListSelected;
        private string[] activeList;
        private string currentFile;
        private int currentFileIndex = 0;


        #endregion

        #region "Public"

        public enum ActiveListItem
        {
            FileList,
            FileListSelected

        }
        public string CurrentFile
        {
            get { return currentFile; }
            set { currentFile = value; }
        }


        public string[] FileListSelected
        {
            get { return fileListSelected; }
            set
            {
                fileListSelected = value;
                activeList = fileListSelected;
                currentFileIndex = 0;
            }
        }

        public string[] FileList
        {
            get { return fileList; }
            set
            {
                fileList = value;
                activeList = fileList;
                currentFileIndex = 0;
            }
        }


        public string FilePath
        {
            get { return filePath; }
            set
            {
                filePath = value;
            }
        }



        public string[] FindFileList(string folderPath)
        {

            string supportedExtensions = "*.html,*.htm";
            return Directory.GetFiles(filePath, "*.*", SearchOption.AllDirectories).Where(s => supportedExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();


        }

        public bool NextFile()
        {


            if (currentFileIndex < activeList.Length - 1)
            {
                currentFileIndex++;
                CurrentFile = activeList[currentFileIndex];
                return true;
            }
            else
            {
                return false;
            }

        }

        public bool PreviousFile()
        {
            if (currentFileIndex > 0)
            {
                currentFileIndex--;
                CurrentFile = activeList[currentFileIndex];
                return true;

            }
            else
            {

                return false;
            }
        }

        #endregion
    }
}
